package com.clqteam.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.clqteam.dao.ShowTempOrderDao;
import com.clqteam.model.Product;
import com.clqteam.util.JDBCutil;

public class ShowTempOrderDaoImpl implements ShowTempOrderDao{
	
	
	@Override
	public int getTotalTempOrder(String tbName) {
		int tot = 0;
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT COUNT(ptId) as total FROM ");
		sql.append(tbName);
		sql.append(" INNER JOIN s_product ON ");
		sql.append(tbName);
		sql.append(".ptId = s_product.pId");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			ResultSet rs = pst.executeQuery();
			if(rs.next()){
				tot = rs.getInt("total");	
			}
			JDBCutil.closeJDBC(pst, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}				
		return tot;
	}


	@Override
	public List<Product> getTempOrder(String tbName) {
		List<Product> pros = new ArrayList<Product>();
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT ");
		sql.append(tbName);
		sql.append(".ptid,s_product.pname,s_product.ptype,s_product.pPrice,");
		sql.append(tbName);		
		sql.append(".ptamount,s_product.pPrice*");
		sql.append(tbName);
		sql.append(".ptamount AS esum FROM ");
		sql.append(tbName);
		sql.append(" INNER JOIN s_product ON ");
		sql.append(tbName);
		sql.append(".ptId = s_product.pId");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				Product pro = new Product();
				pro.setpId(rs.getInt("ptid"));
				pro.setpName(rs.getString("pname"));
				pro.setpType(rs.getString("ptype"));
				pro.setPoAmount(rs.getInt("ptamount"));
				pro.seteSum(rs.getDouble("esum"));
				pro.setpPrice(rs.getDouble("pPrice"));
				pros.add(pro);				
			}
						
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return pros;
	}


	@Override
	public int deleteTempOrderInfoByPId(int pId,String tbName) {
		int flag = -1;
		StringBuffer sql = new StringBuffer();
		sql.append("DELETE FROM ");
		sql.append(tbName);
		sql.append(" WHERE ptId=?");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, pId);
			flag = pst.executeUpdate();
			JDBCutil.closeJDBC(pst, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}			
		return flag;
	}


	@Override
	public int updateTempOrderInfoByPId(int newAmount,int pId, String tbName) {
		int flag = -1;
		StringBuffer sql = new StringBuffer();
		sql.append("UPDATE ");
		sql.append(tbName);
		sql.append(" SET ptamount = ? WHERE ptId = ?");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, newAmount);
			pst.setInt(2, pId);
			flag = pst.executeUpdate();		
			JDBCutil.closeJDBC(pst, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		return flag;
	}

	
}
